//
//  ZYMessageListVieweCell.m
//  YiLin
//
//  Created by Zyark on 2018/6/2.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYMessageListViewCell.h"
#import "ZYMessage.h"
#import "WZLBadgeImport.h"

@interface ZYMessageListViewCell()
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UIImageView *iconImageView;
@property(nonatomic,strong) UILabel *messageTypeLabel;
@property(nonatomic,strong) UILabel *dateLabel;
@property(nonatomic,strong) UILabel *messageLabel;
@property(nonatomic,strong) ZYChangeLanguage *language;
@end

@implementation ZYMessageListViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _language = [ZYChangeLanguage sharedZYChangeLanguage];
        [self setupView];
    }
    return self;
}

-(void)setupView{
    
    UIView *bgView = [[UIView alloc] init];
    UIImageView *iconImageView = [[UIImageView alloc] init];
    UILabel *messageTypeLabel = [[UILabel alloc] init];
    UILabel *dateLabel = [[UILabel alloc] init] ;
    UILabel *messageLabel = [[UILabel alloc] init];
    
    //消息类型
    messageTypeLabel.textColor =kLabelColor;
    messageTypeLabel.font = [UIFont systemFontOfSize:16];
    messageTypeLabel.textAlignment = NSTextAlignmentLeft;
    
    //日期
    dateLabel.textColor = RGBA(153, 153, 153, 1);
    dateLabel.font = [UIFont systemFontOfSize:12];
    dateLabel.textAlignment = NSTextAlignmentRight;
    
    //消息文本
    messageLabel.textColor = RGBA(153, 153, 153, 1);
    messageLabel.font = [UIFont systemFontOfSize:12];
    messageLabel.textAlignment = NSTextAlignmentLeft;
    messageLabel.numberOfLines = 1;
    
    [self.contentView addSubview:bgView];
    [bgView addSubview:iconImageView];
    [bgView addSubview:messageTypeLabel];
    [bgView addSubview:dateLabel];
    [bgView addSubview:messageLabel];
    
    ZYWeakSelf(weakSelf)

    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.equalTo(weakSelf.contentView.mas_top);
        make.bottom.equalTo(weakSelf.contentView).offset(-15);
        //        make.bottom.equalTo(weakSelf.mas_bottom).offset(-15);
    }];
    
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.width.mas_equalTo(45);
//        make.height.mas_equalTo(45);
        make.bottom.mas_equalTo(-15);
    }];
    
    [messageTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(10);
        make.top.equalTo(iconImageView.mas_top);
        make.width.equalTo(weakSelf.mas_width).multipliedBy(0.3);
        make.height.mas_equalTo(20);
    }];
    
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-15);
        make.left.equalTo(messageTypeLabel.mas_right).offset(15);
        make.centerY.equalTo(messageTypeLabel.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(messageTypeLabel.mas_left);
        make.right.equalTo(dateLabel.mas_right).offset(-30);
        make.bottom.equalTo(iconImageView.mas_bottom);
    }];
    
    //图标
    //    iconImageView.layer.cornerRadius = iconImageView.width / 2;
    //    iconImageView.layer.masksToBounds = YES;
    
    _messageLabel = messageLabel;
    _messageTypeLabel = messageTypeLabel;
    _dateLabel = dateLabel;
    _iconImageView = iconImageView;
    _bgView = bgView;
}

-(void)setMessage:(ZYMessage *)message{
    if(message.messageType == ZYMessageTypeDevice){
        _iconImageView.image = [UIImage imageNamed:@"ic_message_type_device"];
        _messageTypeLabel.text = [_language getStringByKey:LC_MESSAGE_TYPE_DEVICE];
    }else if(message.messageType == ZYMessageTypeSystem){
        _iconImageView.image = [UIImage imageNamed:@"ic_message_type_system"];
        _messageTypeLabel.text = [_language getStringByKey:LC_MESSAGE_TYPE_SYSTEM];
    }
    _messageLabel.text = message.message;
    _dateLabel.text = message.dateStr;
}

-(void)setIsNewMessage:(BOOL)isNewMessage{
    _isNewMessage = isNewMessage;
    if(isNewMessage){
        [_iconImageView showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
    }else{
        [_iconImageView clearBadge];
    }
}

@end
